package net.shopxx.entity;

import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Index;
import org.hibernate.search.annotations.Store;

import com.fasterxml.jackson.annotation.JsonProperty;

/**
 * Entity - 淘宝宝贝采集
 * 
 * @author frankie
 * @version 3.0
 */

@Entity
@Table(name = "bmw_product_sku")
@SequenceGenerator(name = "sequenceGenerator", sequenceName = "bmw_product_sku_sequence")
public class StockKeepingUnit extends OrderEntity {

	private static final long serialVersionUID = 4584587827816620012L;
	/**
	 * SKU名称
	 */
	private String skuItem;
	/**
	 * SKU价格
	 */
	private BigDecimal skuPrice;
	/**
	 * 市场价格
	 */
	private BigDecimal marketPrice;
	/**
	 * 库存
	 */
	private Integer stockQuantity;

	/**
	 * 已分配库存
	 */
	private Integer allocatedInventory;
	/**
	 * 商品SKU编码
	 */
	private String merchantCode;
	/**
	 * SKU图片
	 */
	private String image;
	/**
	 * 外键
	 */
	private Product product;

	public StockKeepingUnit() {

	}

	public StockKeepingUnit(BigDecimal skuPrice, String skuItem) {
		this.skuPrice = skuPrice;
		this.skuItem = skuItem;
	}

	public BigDecimal getSkuPrice() {
		return skuPrice;
	}

	public void setSkuPrice(BigDecimal skuPrice) {
		this.skuPrice = skuPrice;
	}

	public BigDecimal getMarketPrice() {
		return marketPrice;
	}

	public void setMarketPrice(BigDecimal marketPrice) {
		this.marketPrice = marketPrice;
	}

	public String getSkuItem() {
		return skuItem;
	}

	public void setSkuItem(String skuItem) {
		this.skuItem = skuItem;
	}

	public Integer getStockQuantity() {
		return stockQuantity;
	}

	public void setStockQuantity(Integer stockQuantity) {
		this.stockQuantity = stockQuantity;
	}

	@Field(store = Store.YES, index = Index.NO)
	@Column(nullable = false)
	public Integer getAllocatedInventory() {
		if (allocatedInventory == null || allocatedInventory < 0) {
			return 0;
		}
		return allocatedInventory;
	}

	public void setAllocatedInventory(Integer allocatedInventory) {
		this.allocatedInventory = allocatedInventory;
	}

	public String getMerchantCode() {
		return merchantCode;
	}

	public void setMerchantCode(String merchantCode) {
		this.merchantCode = merchantCode;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	@JsonProperty
	@NotNull
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(nullable = false, updatable = false)
	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	/**
	 * 获取可用库存
	 * 
	 * @return 可用库存
	 */
	@Transient
	public Integer getAvailableStock() {
		Integer availableStock = null;
		if (getStockQuantity() != null && getAllocatedInventory() != null) {
			availableStock = getStockQuantity() - getAllocatedInventory();
			if (availableStock < 0) {
				availableStock = 0;
			}
		}
		return availableStock;
	}

	public String toString() {
		return "skuPrice:" + skuPrice + " " + "skuItem:" + skuItem;
	}

}
